/*
 * ChatController.java
 * Copyright 2025 Churan Tech, all rights reserved.
 * Churan PROPRIETARY/CONFIDENTIAL, any form of usage is subject to approval.
 */

package com.churan.ai.mcp.client.controller;

import com.churan.ai.core.context.ApiContext;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * Function: ai chat
 *
 * @author churan
 * @date: 2025年5月7日
 */
@RestController
@RequestMapping(ApiContext.API_PATH + "/mcp/client/")
public class ChatController {

    @Resource
    private ChatClient chatClient;

    /**
     * 默认聊天
     *
     * @param msg
     * @return
     */
    @GetMapping("/chat")
    public String chat(@RequestParam(value = "chat-msg") String msg) {
        return chatClient.prompt()
                .user(msg)
                .call()
                .content();
    }

    /**
     * 流式聊天
     *
     * @param msg
     * @return
     */
    @GetMapping(value = "/chat-stream", produces = "text/event-stream")
    public Flux<String> chatStream(@RequestParam(value = "chat-msg") String msg) {
        return chatClient.prompt()
                .user(msg)
                .stream()
                .content();
    }
}
